#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
アクション機能

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_0"
__date__    = "2012/12/01"
__license__ = 'LGPL'

# ------------------------------------------------------------
## インポート
# ------------------------------------------------------------
# Python

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.app import MikanAuthApp

# ------------------------------------------------------------
## 変数
# ------------------------------------------------------------

# ------------------------------------------------------------
## 関数
# ------------------------------------------------------------

# ------------------------------------------------------------
## クラス
# ------------------------------------------------------------
## Mikan XMLRPC アプリケーションクラス
#
class MikanXMLRPCApp(MikanAuthApp):

	# ----------------------------------------------------
	## XMLRPCディスパッチャー フィールドゲッター
	#  @return 取得した値
	def get_xmlrpc_dispatcher(self):
		return self.FXMLRPCDispatcher

	# ----------------------------------------------------
	## コンストラクタ
	#  @param request リクエスト
	def __init__(self, request, dispatcher):
		super(MikanXMLRPCApp, self).__init__(request)

		self.FXMLRPCDispatcher = dispatcher

	# ----------------------------------------------------
	## 処理
	# @return Trueなら続行
	def perform(self):
		if self.request.isgetmethod:
			return self.perform_get()
		elif self.request.ispostmethod:
			return self.perform_post()
		return False

	# ----------------------------------------------------
	## GET処理
	# @return Trueなら続行
	def perform_get(self):
		self.request.contenttype = 'text/plain'

		result = ""
		methods = self.xmlrpcdispatcher.system_listMethods()
		for m in methods:
			text = self.xmlrpcdispatcher.system_methodHelp(m)
			result = u"%s%s\n\t%s\n\n" % (text, m, text)

		self.request.content = str(result)

		return True

	# ----------------------------------------------------
	## POST処理
	# @return Trueなら続行
	def perform_post(self):
		length = int(self.request.environ['CONTENT_LENGTH'])
		request = self.request.environ['wsgi.input'].read(length)
		response = self.xmlrpcdispatcher._marshaled_dispatch(request)

		self.request.contenttype = 'text/xml'
		self.request.content = response

		return True

	# ----------------------------------------------------
	## Property
	xmlrpcdispatcher = ESiPCProperty(get_xmlrpc_dispatcher, None, None)

# ------------------------------------------------------------
## メイン関数
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
